#include "Hero.h"

#include "Item.h"


#define FRAME_HERO  "hero%d_%d.png"
#define BODY_HERO   "hero%d"

Hero::Hero()
{
}

Hero::~Hero()
{

}

void Hero::initGameObjectWith(GamePlayScreen *_gameScreen, CCDictionary *_dict, signed char _group)
{
    GameObject::initGameObjectWith(_gameScreen, _dict);

#ifdef CCOBJECT_DEBUG
        CCLOG("hero dict: \n");
        CCPrettyPrinter visitor;
        _dict->acceptVisitor(visitor);
        CCLog("hero dict: %s\n", visitor.getResult().c_str());
#endif

    this->lifeNumber = DEFAULT_LIFENUMBER;
    group = _group;

    objType = HERO;
    charType = _dict->valueForKey(KEY_TYPE)->intValue();
    flyForce = CCPointMake(0.3f * ScreenManager::sharedScreenMan()->scaleFactor,
                           0.4f * ScreenManager::sharedScreenMan()->scaleFactor);

    curWeapon = WP_STONE;
    for (unsigned char i = 0; i < ITEM_NUM; ++i) {
		itemArr[i] = 0;
    }

    this->createSprite();
    this->createPhysicBody();
    //this->createBalloon();
}

void Hero::resetHero()
{
    this->sprite->setPosition(getPosFromDict(dataDict, sprite->getContentSize()));
    this->sprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()
                                  ->spriteFrameByName(CCString::createWithFormat(FRAME_HERO, charType, 0)->getCString()));
    this->physicBody->SetTransform(b2Vec2(sprite->getPositionX() / PTM_RATIO, sprite->getPositionY() / PTM_RATIO), 0);

    curWeapon = WP_STONE;
    for (unsigned char i = 0; i < ITEM_NUM; ++i) {
        itemArr[i] = 0;
    }
}

void Hero::createSprite()
{
    sprite = new CCSprite();
    sprite->initWithSpriteFrameName(CCString::createWithFormat(FRAME_HERO, charType, 0)->getCString());
    sprite->setPosition(getPosFromDict(dataDict, sprite->getContentSize()));
    sprite->setTag(HERO);
}

void Hero::createPhysicBody()
{
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;

    bodyDef.userData = this;
    if (sprite) {
        bodyDef.position.Set(sprite->getPositionX() / PTM_RATIO, sprite->getPositionY() / PTM_RATIO);
    }
    physicBody = handler->world->CreateBody(&bodyDef);
    //physicBody->SetFixedRotation(true);

    DataAccess::sharedDataAccess()->getFixturesForBody(physicBody,
                                                       CCString::createWithFormat(BODY_HERO, charType)->getCString(), group);
}

void Hero::initControl()
{

}

void Hero::beginContact(b2Fixture *ownFixture, b2Fixture *otherFixture, b2Contact *contact)
{
	PhysicObject *physObj = (PhysicObject*)otherFixture->GetBody()->GetUserData();
	int objTag = physObj->objType;

	if (objTag == ITEM) {
		char itemType = ((Item*)physObj)->type;
		itemArr[itemType] += 1;
	}

    Character::beginContact(ownFixture, otherFixture, contact);
}

void Hero::endContact(b2Fixture *ownFixture, b2Fixture *otherFixture, b2Contact *contact)
{
    Character::endContact(ownFixture, otherFixture, contact);
}

void Hero::applyForce()
{
    if (forceVector.x != 0.f || forceVector.y != 0.f) {
        this->characterFly(b2Vec2(forceVector.x, forceVector.y));
    }
}

void Hero::update(float dt)
{
    this->applyForce();

    Character::update(dt);
}
